﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web.Security;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using com.codebump;

public partial class TapOutXT_TapOutXT : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString.HasKeys())
            {
                if (Request.QueryString["listid"] != String.Empty)
                    Session["listid"] = Request.QueryString["listid"];
                else
                    Session["listid"] = 1;


                if (Request.QueryString["dnis"] != null)
                {
                    Session["dnis"] = Request.QueryString["dnis"];
                }
                else
                {
                    Session["dnis"] = "9999999999";
                }
                Session["ani"] = Request.QueryString["ani"];
                Session["servername"] = Request.QueryString["servername"];
                Session["projectname"] = Request.QueryString["projectname"];
                Session["campaignname"] = Request.QueryString["campaignname"];
                Session["agentid"] = Request.QueryString["agentid"];
                Session["agentfirst"] = Request.QueryString["agentfirst"];
                Session["agentlast"] = Request.QueryString["agentlast"];
                Session["commit"] = false;
                Session["CInfo"] = false;
                Session["App"] = new DataAccess();

                tbPhone.Text = Request.QueryString["ani"].Trim();

            }

            //The session variables are requested in the master page currently using the following syntax: Session["servername"] = Request.QueryString["servername"];
            //The page is loaded before the master page so if the code isn't in a control, it will currently error out.
            //((DataAccess)Session["App"]).ConnectInbound(Session["servername"].ToString(), Session["projectname"].ToString(), Session["campaignname"].ToString(), Session["dnis"].ToString(), Session["listid"].ToString());

        }
        else
        {

        }


    }

    protected void btnSubmit_Click(object sender, System.EventArgs e)
    {
        bool bReturn = submitCustData();
    }


   
 
    #region Validate customer info and write it to in-memory calldata table

    protected bool validateCustomer()
    {
        bool bCustValid = false;
        
        bCustValid = !(string.IsNullOrEmpty(tbFirstName.Text) ||
            string.IsNullOrEmpty(tbLastName.Text) ||
            string.IsNullOrEmpty(tbPhone.Text) ||
            (tbPhone.Text.Length != 10) ||
            !(System.Text.RegularExpressions.Regex.Match(tbPhone.Text, @"^[1-9]\d{2}[1-9]\d{6}$").Success) 
            ); 
          

        return bCustValid;
    }

    protected bool submitCustData()
    {
        bool bSubmited = false;
        lblError.Text = "";

        if (validateCustomer())
        {
            ((DataAccess)Session["App"]).PutCallData("firstname", tbFirstName.Text.Replace(",", ""));
            ((DataAccess)Session["App"]).PutCallData("lastname", tbLastName.Text.Replace(",", ""));
            ((DataAccess)Session["App"]).PutCallData("PrimaryPhone", tbPhone.Text);
            ((DataAccess)Session["App"]).PutCallData("CustomerID", tbCustomerID.Text);           
            ((DataAccess)Session["App"]).CommitCallData();

            bSubmited = true;
        }
        //this else covers error checking required fields
        else
        {
            if (string.IsNullOrEmpty(tbLastName.Text) || string.IsNullOrEmpty(tbFirstName.Text))
            {
                lblError.Text += "Enter a First and Last Name <br/>";
            }
            else
            {
                if (string.IsNullOrEmpty(tbPhone.Text) || !(System.Text.RegularExpressions.Regex.Match(tbPhone.Text, @"^[1-9]\d{2}[1-9]\d{6}$").Success))
                {
                    lblError.Text += "Enter a Valid Phone<br/>";
                }
                else
                {
                    lblError.Text += "Enter Valid Customer Data<br/>";
                }
            }
        }
        return bSubmited;
    }
  
    #endregion Validate customer info and write it to in-memory calldata table

  


}

